package com.example.smartpark.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.smartpark.common.Result;
import com.example.smartpark.dto.AnnouncementDTO;
import com.example.smartpark.dto.AnnouncementQueryDTO;
import com.example.smartpark.service.AnnouncementService;
import com.example.smartpark.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 公告Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/announcements")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 获取公告列表
     *
     * @param queryDTO 查询条件
     * @return 公告列表
     */
    @GetMapping
    public Result<IPage<AnnouncementDTO>> getAnnouncementList(AnnouncementQueryDTO queryDTO) {
        log.info("获取公告列表: {}", queryDTO);
        IPage<AnnouncementDTO> page = announcementService.getAnnouncementPage(queryDTO);
        return Result.success(page);
    }

    /**
     * 获取公告详情
     *
     * @param id 公告ID
     * @param token 用户token
     * @return 公告详情
     */
    @GetMapping("/{id}")
    public Result<AnnouncementDTO> getAnnouncementDetail(
            @PathVariable Long id,
            @RequestHeader(value = "Authorization", required = false) String token) {
        log.info("获取公告详情: {}", id);
        
        // 获取用户ID
        Integer userId = null;
        if (token != null && token.startsWith("Bearer ")) {
            String jwt = token.substring(7);
            userId = JwtUtils.getUserIdFromToken(jwt);
        }
        
        AnnouncementDTO announcement = announcementService.getAnnouncementById(id, userId);
        return Result.success(announcement);
    }
} 